package com.example.cshaoshao.demo.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.cshaoshao.demo.R;

/**
 * Created by cshaoshao on 2017/6/30.
 */

public class FragmentOne extends Fragment implements View.OnClickListener {
    private TextView recTv,tvSport;
    private ReccomendFragment reccomendFragment;
    private SportFragment sportFragment;
    public static FragmentOne getInstance()
    {
        FragmentOne fragmentOne=new FragmentOne();
        return fragmentOne;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view=inflater.inflate(R.layout.activity_fragment,container,false);
        FragmentTransaction transition=getFragmentManager().beginTransaction();
        reccomendFragment=ReccomendFragment.getInstance();
        transition.add(R.id.indexFl,reccomendFragment);
        transition.commit();
        initTextView(view);
        return view;
    }

    private void initTextView(View view) {
        recTv= (TextView) view.findViewById(R.id.rec);
        tvSport= (TextView) view.findViewById(R.id.sport);
       recTv.setOnClickListener(this);
        tvSport.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction transition=getFragmentManager().beginTransaction();
        hideallTransiction(transition);
        switch (v.getId())
        {
            case R.id.rec:
                if(reccomendFragment==null)
                {
                    reccomendFragment=ReccomendFragment.getInstance();
                    transition.add(R.id.indexFl,reccomendFragment);
                }
                else
                transition.show(reccomendFragment);
                break;
            case R.id.sport:
                if(sportFragment==null)
                {
                    sportFragment=SportFragment.getInstance();
                    transition.add(R.id.indexFl,sportFragment);
                }
                else
                    transition.show(sportFragment);
                break;
            default:
                break;
        }
        transition.commit();
    }

    private void hideallTransiction(FragmentTransaction transition) {
        if(transition==null)
        {
            return;
        }
        if(reccomendFragment!=null)
        {
           transition.hide(reccomendFragment);
        }
        if(sportFragment!=null)
        {
            transition.hide(sportFragment);
        }

    }
}
